package com.nondo.dean.platform.service;

import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.platform.domain.Customer;
import com.nondo.dean.platform.domain.SignIn;
import com.nondo.dean.platform.domain.SignInExample;
import com.nondo.dean.platform.mapper.SignInMapper;
import com.nondo.dean.platform.model.CustomerBean;
import com.nondo.dean.platform.model.result.CustomerResult;
import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.session.RowBounds;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author xiaofan
 * @Description: 签到业务管理
 * @throws
 * @date 2018-05-2315:53
 * @Version
 */
@Component
public class SignInService {

    @Autowired
    private SignInMapper signInMapper;

    @Autowired
    private CustomerService customerService;

    /**
     * 获取当天时间 格式 '2018-05-21 00:00:00' '2018-05-21 23:59:59'
     * @return
     */
    public Map<String,Date> getCalendarDate(){
        Map<String,Date> newMap = new HashedMap();
        Calendar beginNow = Calendar.getInstance();
        beginNow.getTime();
        beginNow.set(Calendar.HOUR_OF_DAY,0);
        beginNow.set(Calendar.MINUTE,0);
        beginNow.set(Calendar.SECOND,0);
        Date begin = beginNow.getTime();
        newMap.put("beginDate",begin);
        Calendar endNow = Calendar.getInstance();
        endNow.getTime();
        endNow.set(Calendar.HOUR_OF_DAY,23);
        endNow.set(Calendar.MINUTE,59);
        endNow.set(Calendar.SECOND,59);
        Date end = endNow.getTime();
        newMap.put("endDate",end);
        return newMap;
    }
    /**
     * 新增签到
     * @param signIn
     * @return
     */
    public int add(SignIn signIn){
        return signInMapper.insertSelective(signIn);
    }

    /**
     * 按id查询
     * @param id
     * @return
     */
    public SignIn select(Integer id){
        return signInMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据customerId和当天时间查询
     * @param signIn
     * @return
     */
    public List<SignIn> selectExmple(SignIn  signIn){
        SignInExample example = new SignInExample();
        Map<String,Date> resultMap = getCalendarDate();
        example.createCriteria().andCustomerIdEqualTo(signIn.getCustomerId()).andCreateDateBetween(resultMap.get("beginDate"),resultMap.get("endDate"));
        return signInMapper.selectByExample(example);
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param signIn
     */
    void setCriteria(SignInExample.Criteria criteria, SignIn signIn) {
        if (signIn != null) {
            if (signIn.getId() != null)
                criteria.andIdEqualTo(signIn.getId());
            if (signIn.getCustomerId() != null)
                criteria.andCustomerIdEqualTo(signIn.getCustomerId());
            if(signIn.getCreateDate() != null)
                criteria.andCreateDateEqualTo(signIn.getCreateDate());
        }

    }

    /**
     * 按当天统计
     * @return
     */
    public Long countDay(){
        SignInExample example = new SignInExample();
        Map<String,Date> mapDate = getCalendarDate();
        example.createCriteria().andCreateDateBetween(mapDate.get("beginDate"),mapDate.get("endDate"));
        return signInMapper.countByExample(example);
    }

    /**
     * 签到列表
     * @param offset
     * @param limit
     * @return
     */
    public List<CustomerBean> getSignList(int offset, int limit, SignIn signIn){
        List<CustomerBean> newList = new ArrayList<CustomerBean>();
        RowBounds rowBounds = new RowBounds(offset,limit);
        SignInExample example = new SignInExample();
        example.setOrderByClause("create_date DESC");
        Map<String,Date> mapDate = getCalendarDate();
        example.createCriteria().andCreateDateBetween(mapDate.get("beginDate"),mapDate.get("endDate"));
        List<SignIn> signInList = signInMapper.selectByExampleWithRowbounds(example,rowBounds);
        for(SignIn signInBean : signInList){
            CustomerBean customerBean = new CustomerBean();
            if(signInBean.getCustomerId() !=null){
                Customer beans = customerService.selectId(signInBean.getCustomerId()) == null ?  new Customer() : customerService.selectId(signInBean.getCustomerId());
                BeanUtils.copy(beans,customerBean);
            }
            newList.add(customerBean);
        }

        return newList;
    }

}
